﻿/**************************************************************************************************\
++++++++++++++++++++++++++++++ ARKADIUM INTELLECTUAL PROPERTY TERMS ++++++++++++++++++++++++++++++
The Software is protected by US copyright laws and international treaties. Licensee must treat the 
Software like any other copyrighted material – for example a book. Licensee acknowledges that 
Arkadium owns the Software and that nothing set forth herein conveys to Licensee any ownership in 
or to the Software, including without any Derivative Works of the Software.  
++++++++++++++++++++++++++++++ ARKADIUM INTELLECTUAL PROPERTY TERMS ++++++++++++++++++++++++++++++

AUTHOR: Neil Mrva

PURPOSE: This class manages all aspects of content panels being shown after the game loads. It's 
resposible for depth managment and showing/hiding panels when requested.
\**************************************************************************************************/

import arkadium.GUIPanel;
import mx.utils.Delegate;



class arkadium.GUIManager extends MovieClip
{
	private var _smcScrim:MovieClip;
	public var _aGUIPanels:Array; // stores all GUIPanels that have been registered.
	private var _aVisiblePanels:Array; // stores references to visible panels ONLY.
	
	
	public function GUIManager()
	{
		
		// Setup scrim for use with small window panels.
		_smcScrim.useHandCursor = false;
		_smcScrim.onPress = function() {}
		_smcScrim.onRelease = function() {}
		_smcScrim.onReleaseOutside = function() {}
		_smcScrim.onRollOver = function() {}

		_aGUIPanels = new Array();
		_aVisiblePanels = new Array();
		
	}
	
	/**************************************************************************************************\
	onInitialize - Called by main to initialize anything that couldn't be handled by the constructor.
	\**************************************************************************************************/
	public function onInitialize()
	{
		// Let's initialize all registered GUIPanels.
		trace("initting all these panels"+_aGUIPanels.length)
		for ( var i:Number = 0; i < _aGUIPanels.length; ++i )
		{
			_aGUIPanels[ i ].onInitialize();
		}
	}
		
	/**************************************************************************************************\
	registerPanel - Called by children GUI panels to register themselves with the manager.
	\**************************************************************************************************/
	public function registerPanel( cPanelInstance:GUIPanel, sPanelName:String ):Boolean
	{
		if ( sPanelName )
		{
			if ( !getPanelInstance( sPanelName ) )
			{
				// TODO: should be able to disable any panel that registers as a debug panel.
				
				cPanelInstance._visible = false;
				_aGUIPanels.push( cPanelInstance );
				trace("registerd panel "+ sPanelName);
				return true;
			}
			else
			{
				//trace( "GUIManager: registerPanel() - GUI Panel \"" + sPanelName + "\" already exists and was not registered." );
			}
		}
		else
		{
			//trace( "GUIManager: registerPanel() - A GUI Panel name was not provided ans was not registered." );
		}
		
		return false;
	}
	

	/**************************************************************************************************\
	getPanelInstance - This function is used to find and return registered GUI Panels.
	\**************************************************************************************************/
	public function getPanelInstance( sPanelName:String ):GUIPanel
	{
		for ( var i:Number = 0; i < _aGUIPanels.length; ++i )
		{
			if ( sPanelName == _aGUIPanels[ i ].getPanelName() )
			{
				return _aGUIPanels[ i ];
			}
		}
		
		return null;
	}

	/**************************************************************************************************\
	showPanel - This function is used internally to show a registered GUIPanel.
	\**************************************************************************************************/
	public function showPanel( sPanelName:String, oParams:Object ):GUIPanel
	{
		if ( sPanelName == null )
		{
			//trace( "GUIManager: showPanel() - A GUI Panel name was not provided and can't be shown." );
		}
		
		// Let's retrieve the panel in case it's visible.
		var cCurrentPanel:GUIPanel = removeVisiblePanel( sPanelName );
				
		// If the panel is not visible, let's search for it.
		if ( !cCurrentPanel )
		{
			cCurrentPanel = getPanelInstance( sPanelName );
			
			if ( !cCurrentPanel )
			{
				//trace( "GUIManager: showPanel() - GUI Panel \"" + sPanelName + "\" does not exist and can't be shown." );
				return null;
			}
		}
		
		// If the specified panel is found, let's push it to the top and show it.
		_aVisiblePanels.push( cCurrentPanel );
		reorderVisiblePanels();
		cCurrentPanel.showPanel();
		cCurrentPanel._visible = true;
		return cCurrentPanel;
	}
	
	/**************************************************************************************************\
	removeVisiblePanel - This function is used to remove a specified panel that is visible and returns 
	it if found.
	\**************************************************************************************************/
	private function removeVisiblePanel( sPanelName:String ):GUIPanel
	{
		for ( var i:Number = _aVisiblePanels.length - 1; i >= 0; --i )
		{
 			if ( sPanelName == _aVisiblePanels[ i ].getPanelName() )
			{
				return _aVisiblePanels.splice( i, 1 )[ 0 ];
			}
		}
		
 		return null;
	}
	
	/**************************************************************************************************\
	reorderVisiblePanels - This function is called to reorder the depths of all visible panels and set 
	the scrim under the top most panel.
	\**************************************************************************************************/
	private function reorderVisiblePanels()
	{
		// Reorder the panels based on their index starting from 0.
		for ( var i:Number = 0; i < _aVisiblePanels.length; ++i )
		{
			_aVisiblePanels[ i ].swapDepths( i * 2 );
			//trace( "NAME: " + _aVisiblePanels[ i ]._name + ", DEPTH: " + _aVisiblePanels[ i ].getDepth() );
		}
		
		// Automatically place the scrim below the top most panel.
		_smcScrim.swapDepths( ( ( _aVisiblePanels.length - 1 ) * 2 ) - 1 );
	}
	
	/**************************************************************************************************\
	reorderVisiblePanels - This function is called to reorder the depths of all visible panels and set 
	the scrim under the top most panel.
	\**************************************************************************************************/
	public function hidePanel( sPanelName:String )
	{
		if ( sPanelName == null )
		{
			//trace( "GUIManager: showPanel() - A GUI Panel name was not provided and can't be hidden." );
		}
		
		// Let's retrieve the panel.
		var cCurrentPanel:GUIPanel = removeVisiblePanel( sPanelName );
		
		// If the panel is visible, let's hide it and reorder the list.
		if ( cCurrentPanel )
		{
			reorderVisiblePanels();
			cCurrentPanel.hidePanel();
			cCurrentPanel._visible = false;
		}
	}
	
	/*
	public function hideAllPanels()
	{
		var cCurrentPanel:GUIPanel;
		
		while ( _aVisiblePanels.length )
		{
			cCurrentPanel = GUIPanel( _aVisiblePanels.shift() );
			cCurrentPanel.hidePanel();
			cCurrentPanel._visible = false;
		}
	}
	*/
	
	
}